package icu.javacg.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import icu.javacg.storage.pojo.Environment;
import icu.javacg.storage.pojo.vo.ReturnResult;
import icu.javacg.storage.service.EnvironmentService;
import icu.javacg.storage.mapper.EnvironmentMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author sky
 * @description 针对表【environment(温湿度记录表)】的数据库操作Service实现
 * @createDate 2023-04-25 08:42:44
 */
@Service
@Slf4j
public class EnvironmentServiceImpl extends ServiceImpl<EnvironmentMapper, Environment>
        implements EnvironmentService {

    @Resource
    private EnvironmentMapper environmentMapper;

    /**
     * 通过湿度值获取环境数据
     *
     * @param humidityValue 湿度值
     * @return ReturnResult<List < Environment>>
     */
    @Override
    public ReturnResult<List<Environment>> getEnvironmentByHumidityValue(Double humidityValue) {
        QueryWrapper<Environment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("humidity_value", humidityValue);
        List<Environment> environments = environmentMapper.selectList(queryWrapper);
        if (environments.size() == 0) {
            return new ReturnResult<>(1001, "通过湿度值获取环境数据，查询失败，没有数据");
        }
        return new ReturnResult<>(1501, "通过湿度值获取环境数据，查询成功", environments);
    }

    /**
     * 通过温度值获取环境数据
     *
     * @param temperatureValue 温度值
     * @return ReturnResult<List < Environment>>
     */
    @Override
    public ReturnResult<List<Environment>> getEnvironmentByTemperatureValue(Double temperatureValue) {
        QueryWrapper<Environment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("temperature_value", temperatureValue);
        List<Environment> environments = environmentMapper.selectList(queryWrapper);
        if (environments.size() == 0) {
            return new ReturnResult<>(1002, "通过温度值获取环境数据，查询失败，没有数据");
        }
        return new ReturnResult<>(1502, "通过温度值获取环境数据，查询成功", environments);
    }

    /**
     * 通过条件获取环境数据
     *
     * @param environment 温度湿度记录表实体
     * @return ReturnResult<List < Environment>>
     */
    @Override
    public ReturnResult<List<Environment>> getEnvironmentByCondition(Environment environment) {
        boolean isSelect = false;
        QueryWrapper<Environment> queryWrapper = new QueryWrapper<>();
        if (environment.getTemperatureRecordId() != null && environment.getTemperatureRecordId() != 0) {
            queryWrapper.eq("temperature_record_id", environment.getTemperatureRecordId());
            isSelect = true;
        }
        if (environment.getTemperatureRecordTime() != null) {
            queryWrapper.eq("temperature_record_time", environment.getTemperatureRecordTime());
            isSelect = true;
        }
        if (environment.getTemperatureValue() != null && environment.getTemperatureValue() != 0.0) {
            queryWrapper.eq("temperature_value", environment.getTemperatureValue());
            isSelect = true;
        }
        if (environment.getTemperatureRecordLocation() != null && !environment.getTemperatureRecordLocation().equals("")) {
            queryWrapper.eq("temperature_record_location", environment.getTemperatureRecordLocation());
            isSelect = true;
        }
        if (environment.getHumidityRecordId() != null && environment.getHumidityRecordId() != 0) {
            queryWrapper.eq("humidity_record_id", environment.getHumidityRecordId());
            isSelect = true;
        }
        if (environment.getHumidityRecordTime() != null) {
            queryWrapper.eq("humidity_record_time", environment.getHumidityRecordTime());
            isSelect = true;
        }
        if (environment.getHumidityValue() != null && environment.getHumidityValue() != 0.0) {
            queryWrapper.eq("humidity_value", environment.getHumidityValue());
            isSelect = true;
        }
        if (environment.getHumidityRecordLocation() != null && !environment.getHumidityRecordLocation().equals("")) {
            queryWrapper.eq("humidity_record_location", environment.getHumidityRecordLocation());
            isSelect = true;
        }
        if (isSelect) {
            List<Environment> environments = environmentMapper.selectList(queryWrapper);
            if (environments.size() == 0) {
                return new ReturnResult<>(1003, "条件查询失败，没有数据");
            }
            return new ReturnResult<>(1503, "条件查询成功", environments);
        }else {
            return new ReturnResult<>(1003, "条件查询失败，条件不能为空");
        }
    }

    /**
     * 新增环境数据
     * @param environment
     * @return
     */
    @Override
    public ReturnResult<Environment> addEnvironment(Environment environment) {
        System.out.println(environment);
        if (environment.getHumidityRecordId() != null && environment.getHumidityRecordId() != 0 ) {
            if (environment.getTemperatureRecordId() != null && environment.getTemperatureRecordId() != 0) {
                if (environment.getTemperatureRecordId().equals(environment.getHumidityRecordId())) {
                    try {
                        if (environmentMapper.insert(environment) > 0) return new ReturnResult<>(1504, "添加成功");
                    }catch (Exception e) {
                        log.error(e.getLocalizedMessage());
                        return new ReturnResult<>(1004, "添加失败，环境温度或湿度ID重复");
                    }
                }
                return new ReturnResult<>(1004, "添加失败，环境温度ID和环境湿度ID必须相同");
            }
            return new ReturnResult<>(1004, "添加失败，环境温度ID不能为空或为0");
        }
        return new ReturnResult<>(1004, "添加失败，环境湿度ID不能为空或为0");
    }

}




